<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="未销售合同商品"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <unsold ref="unsold"></unsold>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
  <script>
import VolBox from "@/components/basic/VolBox.vue";
import unsold from "@/views/salesinfo/unsold/q_unsold.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    unsold,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.unsold.searchFormFields.Sales_contract_voucher_sub = null;
      this.$refs.unsold.searchFormFields.Goods_sub = null;
      this.$refs.unsold.searchFormFields.Goods_name = null;
      this.$refs.unsold.searchFormFields.Goods_num = [null, null];
    },
    open(Voucher_sub) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.unsold) {
          clearInterval(setIntervalTiem);
          this.$refs.unsold.searchFormFields.Sales_contract_voucher_sub =
            Voucher_sub;
          this.$refs.unsold.$refs.grid.search();
        }
      });
    },

  async  onSelect() {
        let rowObj = this.$refs.unsold.$refs.grid.getSelectRows();
        if (!rowObj.length) {
          this.base.ElMessageFn("请至少选择一条数据");
          return (this.model = true);
        }
        // 处理无库存
        let Sales_contract_voucher_sub= this.$refs.unsold.searchFormFields.Sales_contract_voucher_sub
        let list =await rowObj.filter(
          (item) =>
            item.库位 != null &&
            item.Sales_contract_voucher_sub ==Sales_contract_voucher_sub
        ); //有库存
        let list2 =await rowObj.filter((item) => item.库位 == null); //无有库存
        let list3 =await rowObj.filter(
          (item) =>
            item.Sales_contract_voucher_sub !=Sales_contract_voucher_sub
        );//非当前合同商品
       try {
        if (list2.length) {
          let str = "";
          list2.forEach((r) => {
            if (str == "") str += r.Goods_name;
            else str = str + "," + r.Goods_name;
          });
          this.$message.warning(`商品${str}无库存`);
        }
        if (list3.length) {
          let str = "";
          list3.forEach((r) => {
            if (str == "") str += r.Goods_name;
            else str = str + "," + r.Goods_name;
          });
          this.$message.warning(
            `商品${str}非当前合同商品,未添加到本销售单`
          );
        }
       } catch (error) {
        console.log(error)
       }
    
      this.$emit("parentCall", ($parent) => {
        try {
          //处理明细表已含有相同数据
          let parentList = $parent.$refs.detail.rowData;
          let set = new Set(parentList.map((item) => item.Delivery_requestList));//得到由明细表goods_sub组成的数组
          let commonObjects = list.filter((item) => set.has(item.Delivery_requestList)); //得到明细表中已包含的选中行
          let goodsNamesSet = new Set( commonObjects.map((item) => item.Delivery_requestList)); //相同商品的编码
          if (commonObjects.length) {
            list = list.filter((item) => !goodsNamesSet.has(item.Delivery_requestList)); //在选中数据中剔除明细表已包含的行数据
            let str = "";
            commonObjects.forEach((r) => {
              if (str == "") str += r.Goods_name;
              else str = str + "," + r.Goods_name;
            });
            this.$message.warning(`商品${str}已存在此销售单`);
          }
          let arr = list.map((item) => {
            return {
              Goods_sub: item.Goods_sub,
              Goods_name: item.Goods_name,
              Goods_num: item.Goods_num,
              Total_price: item.Total_price,
              Tax_exclusive_contract_total_price:
                item.Tax_exclusive_total_price,
              WHSE_name: item.库位,
              AvailableQuantity: item.Contract_goods_num,
              sales_contractList_ID: item.sales_contractList_ID,
              Sales_contract_voucher_sub: item.Sales_contract_voucher_sub,
              Inventory_ID: item.库存ID,
              Delivery_requestList: item.Delivery_requestList,
            };
          });
          $parent.$refs.detail.rowData.unshift(...arr);
        } catch (error) {
          console.error(error);
        }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>